import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'/'el-icon-x' the icon show in the sidebar
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
// 路由种类:
// 1. 静态路由 constantRoutes: 所有人都有的页面
// 2. 动态路由 asyncRoutes: 每个菜单不一样,需要权限控制
export const constantRoutes = [{
  path: '/login',
  component: () => import('@/views/login/index'),
  hidden: true
},

{
  path: '/404',
  component: () => import('@/views/404'),
  hidden: true
},

{
  path: '/',
  component: Layout,
  redirect: '/dashboard',
  children: [{
    path: 'dashboard',
    name: 'Dashboard',
    component: () => import('@/views/dashboard/index'),
    meta: {
      title: '首页',
      icon: 'dashboard'
    }
  }]
},
{
  path: '/import',
  component: Layout,
  hidden: true,
  children: [{
    path: '',
    component: () => import('@/views/import/index.vue')
  }]
},
{
  path: '/learnVuex',
  component: () => import('@/views/learnVuex/index'),
  hidden: true
}
// 404 page must be placed at the end !!!
// {
//   path: '*',
//   redirect: '/404',
//   hidden: true
// }
]

// 这里是动态路由
import employees from './modules/employees.js'
import approvals from './modules/approvals.js'
import attendances from './modules/attendances.js'
import department from './modules/department.js'
import permission from './modules/permission.js'
import salary from './modules/salary.js'
import setting from './modules/setting.js'
import social from './modules/social.js'
export const asyncRoutes = [
  // 员工
  // 将下面注释掉的这部分单独写到模块化中
  // {
  // path: '/emplyees',
  // component: Layout,
  // children: [{
  //   // 默认路径,访问父路由/employees直接访问这个子路由
  //   path: '',
  //   name: 'Employees',
  //   component: () => import('@/views/employees/index.vue'),
  //   meta: {
  //     title: '员工', icon: 'dashboard'
  //   }
  // }]
  // }
  employees, approvals, attendances, department, permission, salary, setting, social
]
// 最后将静态路由和动态路由合并到一起输出~
const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({
    y: 0
  }),
  // 在后面菜单权限的时候,要根据最开始的用户信息分配角色,再根据角色分配对应的权限,来显示对应的菜单.
  // 所以需要对动态路由进行过滤,故在presssions.js中设置路由守卫的时候,对动态路由进行过滤处理
  // 处理完成之后,在路由守卫的地方对动态和静态路由进行合并
  // routes: [...constantRoutes, ...asyncRoutes]
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
